$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.funObj().addData();//获取select数据
            this.listener();
        },
        listener:function () {
            var $this = this;
            $('#doConfirm').on('click',function () {
                $this.funObj().showData();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                addData: function () {
                    xhExtra.ajaxRequest('res/getAllSchoolData', {}, {check:true}, function (data) {
                        $('#school').select2({placeholder: '选择学校', data: data, allowClear: true, width:150});
                    });
                    $this.funObj().showData();
                },
                showData: function () {
                    var school = $('#school').val();
                    xhExtra.ajaxRequest('res/getTeaStatData', {school: school}, {check:true}, function (data) {
                        var str = '<tr class="info teaTitle">' +
                            '<th style="width: 20%;text-align: center;">学校</th>' +
                            '<th style="width: 20%;text-align: center;">科目</th>' +
                            '<th style="width: 20%;text-align: center;">年级</th>' +
                            '<th style="width: 20%;text-align: center;">已申请教师</th>' +
                            '<th style="width: 20%;text-align: center;">未申请教师</th>' +
                            '</tr>';
                        if(data!=null && data.length>0 ) {
                            $.each(data, function (index, row) {
                                str += '<tr style="background-color: #f9f9f9;">';
                                if (row.firstSchRow) {
                                    str += '<td style="text-align: center; vertical-align: middle !important;" rowspan="' + row.schoolRowSpan + '">' + row.sSchoolName + '</td>';
                                }
                                if (row.firstSubRow) {
                                    str += '<td style="text-align: center; vertical-align: middle !important;" rowspan="' + row.subjectRowSpan + '">' + row.subjectName + '</td>';
                                }
                                str += '<td style="text-align: center; vertical-align: middle !important;">' + row.className + '</td>';
                                str += '<td style="text-align: right;" class="done" title="查看申请详情" ids="'+row.showId+'" showName="'+row.showName+'">' + row.done + '</td>';
                                str += '<td style="text-align: right;" class="noDone" title="查看老师" id="'+row.showId+'">' + row.noDone + '</td>';
                                str += '</tr>'
                            })
                        }
                        $('#showStatData').find('table').empty().append(str);

                        /**
                         * 动态加载后的操作放到加载方法中，否则加载后的内容无法监听该动作
                         */
                        //查看申请详情
                        $('.done').click(function () {
                            $this.funObj().openDetail1($(this).attr("ids"), $(this).attr("showName"));
                        });
                        //查看未申请老师
                        $('.noDone').click(function () {
                            $this.funObj().openDetail2($(this).attr("id"));
                        });
                    });
                },
                openDetail1:function (id, name) {
                    if (id === '') {
                        window.top.swal('错误', '组合id不存在', 'info');
                        return false;
                    }
                    window.top.xhTop.createNewTab(name,'res/referenceApply?comId='+id);
                },
                openDetail2:function (id) {
                    if (id === '') {
                        window.top.swal('错误', '组合id不存在', 'info');
                        return false;
                    }
                    $.ajax({
                        url: 'getOutApplyTea',
                        type: 'POST',
                        timeout: 2000,
                        async: false,
                        data: {teachApplyId:id},
                        success: function(data){
                            window.top.xhTop.showMessage({
                                title: '老师详情',
                                text: data.msg
                            });
                        },
                        error: function(){
                            window.top.xhTop.showMessage({
                                title: '发生错误'
                            });
                        }
                    });
                }
            }
        }
    }
};